{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 1. 二进制文件读写\n",
    "\n",
    "- np.save():以二进制的方式保存文件到数据中\n",
    "- np.load():加载二进制文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data: \n",
      " [[ 0.2618988   0.30479602  0.7307923   0.49616699  0.47594052]\n",
      " [ 0.02397009  0.33996271  0.62041845  0.89919974  0.08377456]\n",
      " [ 0.83987029  0.66270435  0.57482151  0.39971126  0.20423299]\n",
      " [ 0.23764856  0.13435936  0.79198312  0.04730726  0.25825767]\n",
      " [ 0.09417188  0.85382959  0.5154922   0.62929393  0.99647136]]\n",
      "\n",
      " load_data_b: \n",
      " [[ 0.2618988   0.30479602  0.7307923   0.49616699  0.47594052]\n",
      " [ 0.02397009  0.33996271  0.62041845  0.89919974  0.08377456]\n",
      " [ 0.83987029  0.66270435  0.57482151  0.39971126  0.20423299]\n",
      " [ 0.23764856  0.13435936  0.79198312  0.04730726  0.25825767]\n",
      " [ 0.09417188  0.85382959  0.5154922   0.62929393  0.99647136]]\n",
      "\n",
      " load_data_txt: \n",
      " [[ 0.2618988   0.30479602  0.7307923   0.49616699  0.47594052]\n",
      " [ 0.02397009  0.33996271  0.62041845  0.89919974  0.08377456]\n",
      " [ 0.83987029  0.66270435  0.57482151  0.39971126  0.20423299]\n",
      " [ 0.23764856  0.13435936  0.79198312  0.04730726  0.25825767]\n",
      " [ 0.09417188  0.85382959  0.5154922   0.62929393  0.99647136]]\n"
     ]
    }
   ],
   "source": [
    "data = np.random.rand(5, 5)\n",
    "print(\"data: \\n\", data)\n",
    "\n",
    "# 以二进制保存数据\n",
    "np.save(\"data.b\", data)\n",
    "# 以文本形式保存数据\n",
    "np.savetxt(\"data.txt\", data)\n",
    "\n",
    "# 读取二进制文本\n",
    "load_data_b = np.load(\"data.b.npy\")\n",
    "print(\"\\n load_data_b: \\n\", load_data_b)\n",
    "# 读取文本形式的文本\n",
    "load_data_txt = np.loadtxt(\"data.txt\")\n",
    "print(\"\\n load_data_txt: \\n\", load_data_txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.读取文件中列表形式的数据\n",
    "\n",
    "- 读取csv格式的数据\n",
    "- np.genfromtxt(\"文件名\", delimite=\"分隔符\", names=是否有标题)\n",
    "- np.genfromtxt的优点：能处理文件中丢失的数据,把空值自动填充为nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting data.csv\n"
     ]
    }
   ],
   "source": [
    "%%writefile data.csv\n",
    "id,value1,value2,value3\n",
    "1,123,1.4,23\n",
    "2,110,0.5,18\n",
    "3,164,2.1,19\n",
    "4,,0.39,20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data: \n",
      " [( 1.,  123.,  1.4 ,  23.) ( 2.,  110.,  0.5 ,  18.)\n",
      " ( 3.,  164.,  2.1 ,  19.) ( 4.,   nan,  0.39,  20.)]\n",
      "data:  [ 1.  2.  3.  4.]\n",
      "data:  ( 3.,  164.,  2.1,  19.)\n",
      "data:  3.0\n",
      "data:  3.0\n"
     ]
    }
   ],
   "source": [
    "# 读取csv文件\n",
    "data = np.genfromtxt(\"data.csv\", delimiter=',', names=True)\n",
    "print(\"data: \\n\", data)\n",
    "\n",
    "# 访问数据\n",
    "print(\"data: \", data[\"id\"])\n",
    "print(\"data: \", data[2])\n",
    "print(\"data: \", data[\"id\"][2])\n",
    "print(\"data: \", data[2][\"id\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
